import { useCallback } from 'react';
import { useNavigate } from 'react-router-dom';
import styled from '@emotion/styled';
import { motion } from 'framer-motion';
import Particles from "react-tsparticles";
import { loadFull } from "tsparticles";
import type { Container, Engine } from "tsparticles-engine";

const PageContainer = styled.div`
  width: 100%;
  min-height: 100vh;
  background: linear-gradient(180deg, #3A8DFF 0%, #0057D9 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
`;

const ContentWrapper = styled(motion.div)`
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 1;
  width: 100%;
  max-width: 100vw;
  padding: 0 20px;
  box-sizing: border-box;
`;

const WelcomeText = styled.h1`
  font-family: "Microsoft YaHei", "Poppins", "Roboto", sans-serif;
  font-size: 36px;
  font-weight: bold;
  color: #FFFFFF;
  text-align: center;
  line-height: 1.5;
  text-shadow: 0 0 2px rgba(255, 255, 255, 0.5);
  margin-bottom: 48px;
  width: 100%;

  @media (max-width: 768px) {
    font-size: 24px;
    line-height: 1.4;
  }
`;

const ButtonContainer = styled.div`
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 24px;
  width: 100%;
  max-width: 600px;
  margin-bottom: 32px;

  @media (max-width: 768px) {
    gap: 12px;
  }
`;

const Button = styled(motion.button)`
  height: 60px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #FFFFFF;
  font-size: 18px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.2s ease;
  width: 100%;

  &:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  @media (max-width: 768px) {
    height: 50px;
    font-size: 16px;
  }
`;

const ParticlesContainer = styled.div`
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
`;

const MainPage = () => {
  const navigate = useNavigate();

  const particlesInit = useCallback(async (engine: Engine) => {
    await loadFull(engine);
  }, []);

  const particlesLoaded = useCallback(async (container: Container | undefined) => {
    // 粒子加载完成后的回调
  }, []);

  return (
    <PageContainer>
      <ParticlesContainer>
        <Particles
          id="tsparticles"
          init={particlesInit}
          loaded={particlesLoaded}
          options={{
            background: {
              opacity: 0
            },
            fullScreen: {
              enable: true,
              zIndex: 0
            },
            fpsLimit: 60,
            particles: {
              color: {
                value: "#ffffff"
              },
              links: {
                enable: false
              },
              move: {
                enable: true,
                direction: "none",
                outModes: "out",
                random: true,
                speed: 0.8,
                straight: false
              },
              number: {
                density: {
                  enable: true,
                  area: 800
                },
                value: 90
              },
              opacity: {
                value: { min: 0.3, max: 0.6 }
              },
              shape: {
                type: "circle"
              },
              size: {
                value: { min: 1, max: 3 }
              }
            },
            detectRetina: true
          }}
        />
      </ParticlesContainer>
      
      <ContentWrapper
        initial={{ opacity: 0, y: 30 }}
        animate={{ opacity: 1, y: 0 }}
        transition={{ duration: 0.6, delay: 0.1 }}
      >
        <WelcomeText>你好呀，欢迎来到你的情感空间 ✨</WelcomeText>
        
        <ButtonContainer>
          <Button whileTap={{ scale: 0.95 }} onClick={() => navigate('/aitalk')}>Ai 聊天室</Button>
          <Button whileTap={{ scale: 0.95 }} onClick={() => navigate('/record')}>心情日记</Button>
          <Button whileTap={{ scale: 0.95 }} onClick={() => navigate('/breath')}>冥想呼吸</Button>
          <Button whileTap={{ scale: 0.95 }} onClick={() => navigate('/know')}>小知识</Button>
          <Button whileTap={{ scale: 0.95 }} onClick={() => navigate('/home')}>我的</Button>
        </ButtonContainer>
      </ContentWrapper>
    </PageContainer>
  );
};

export default MainPage;
